package com.alliky.core.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import com.alliky.core.R
import com.alliky.core.ext.clickWithTrigger
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.toolbar_layout.view.*

/**
 * ================================================
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2022/11/10 11:13
 * ================================================
 */
class TitleBar @JvmOverloads constructor(
    context: Context, val attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : Toolbar(context, attrs, defStyleAttr), LayoutContainer {
    override val containerView: View?
        get() = this


    private lateinit var _title: Title

    /**
     * 左
     */
    private lateinit var _left: Left
    private lateinit var _right: Right


    init {
        inflate(context, R.layout.toolbar_layout, this)
        init()
    }

    private fun init() {
        val a = context.obtainStyledAttributes(attrs, R.styleable.my_toolbar)
        try {
            initToolbarTitle(a)
            initLeft(a)
            initRight(a)
        } finally {
            a.recycle()
        }
    }

    private fun initToolbarTitle(a: TypedArray) {
        if (a.hasValue(R.styleable.my_toolbar_title_text)) {
            val text = a.getString(R.styleable.my_toolbar_title_text)
            tvToolbarTitle.text = text
            // 字体大小
            val textSize = a.getDimensionPixelSize(R.styleable.my_toolbar_title_text_size, -1)
            if (textSize > -1) {
                tvToolbarTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
            }
            // 字体颜色
            val textColor =
                a.getColor(R.styleable.my_toolbar_title_text_color, Color.parseColor("#FFFFFF"))
            tvToolbarTitle.setTextColor(textColor)
        }
    }

    private fun initLeft(a: TypedArray) {
        // 文字
        if (a.hasValue(R.styleable.my_toolbar_left_text)) {
            val text = a.getString(R.styleable.my_toolbar_left_text)
            tvToolbarLeft.text = text
            // 字体大小
            val textSize = a.getDimensionPixelSize(R.styleable.my_toolbar_left_text_size, -1)
            if (textSize > -1) {
                tvToolbarLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
            }
            // 字体颜色
            val textColor = a.getColor(R.styleable.my_toolbar_left_text_color, -1)
            tvToolbarLeft.setTextColor(textColor)
        }

        // 图标
        if (a.hasValue(R.styleable.my_toolbar_left_drawable)) {
            val drawableId = a.getResourceId(R.styleable.my_toolbar_left_drawable, 0)
            // tint变色
            if (a.hasValue(R.styleable.my_toolbar_left_drawable_tint)) {
                val colorTint =
                    a.getColor(R.styleable.my_toolbar_left_drawable_tint, Color.WHITE)
                val drawable = ContextCompat.getDrawable(context, drawableId)
                if (drawable != null) {
                    val wrap = DrawableCompat.wrap(drawable).mutate()
                    DrawableCompat.setTint(wrap, colorTint)
                    tvToolbarLeft.setCompoundDrawablesRelativeWithIntrinsicBounds(
                        wrap,
                        null,
                        null,
                        null
                    )
                }
            } else {
                tvToolbarLeft.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableId, 0, 0, 0)
            }
            // drawablePadding
            if (a.hasValue(R.styleable.my_toolbar_left_drawable_padding)) {
                val padding =
                    a.getDimensionPixelSize(R.styleable.my_toolbar_left_drawable_padding, 0)
                tvToolbarLeft.compoundDrawablePadding = padding
            }
        } else {
            tvToolbarLeft.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0)
        }
    }

    private fun initRight(a: TypedArray) {
        val rightTextView = tvToolbarRight

        // 文字
        if (a.hasValue(R.styleable.my_toolbar_right_text)) {
            val text = a.getString(R.styleable.my_toolbar_right_text)
            rightTextView.text = text
            // 字体大小
            val textSize = a.getDimensionPixelSize(R.styleable.my_toolbar_right_text_size, -1)
            if (textSize > -1) {
                rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
            }
            // 字体颜色
            val textColor =
                a.getColor(R.styleable.my_toolbar_right_text_color, Color.parseColor("#272727"))
            rightTextView.setTextColor(textColor)
        }

        // 图标
        if (a.hasValue(R.styleable.my_toolbar_right_drawable)) {
            val drawableId = a.getResourceId(R.styleable.my_toolbar_right_drawable, 0)

            // tint变色
            if (a.hasValue(R.styleable.my_toolbar_right_drawable_tint)) {
                val colorTint =
                    a.getColor(R.styleable.my_toolbar_right_drawable_tint, Color.WHITE)
                val drawable = ContextCompat.getDrawable(context, drawableId)
                if (drawable != null) {
                    val wrap = DrawableCompat.wrap(drawable).mutate()
                    DrawableCompat.setTint(wrap, colorTint)
                    rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(
                        wrap,
                        null,
                        null,
                        null
                    )
                }
            } else {
                rightTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawableId, 0, 0, 0)
            }
            // drawablePadding
            if (a.hasValue(R.styleable.my_toolbar_right_drawable_padding)) {
                val padding =
                    a.getDimensionPixelSize(R.styleable.my_toolbar_right_drawable_padding, 0)
                rightTextView.compoundDrawablePadding = padding
            }
        }
    }

    fun title(): Title {
        if (!::_title.isInitialized) {
            _title = Title(tvToolbarTitle)
        }
        return _title
    }

    fun left(): Left {
        if (!::_left.isInitialized) {
            _left = Left(tvToolbarLeft)
        }
        return _left
    }

    fun right(): Right {
        if (!::_right.isInitialized) {
            _right = Right(tvToolbarRight)
        }
        return _right
    }


    class Title constructor(val view: AppCompatTextView) {

        fun text(text: String): Title {
            view.text = text
            return this
        }
    }


    class Left constructor(val view: AppCompatTextView) {

        fun onClick(onClick: OnClickListener): Left {
            view.clickWithTrigger { onClick.onClick(it) }
            return this
        }
    }

    class Right(private val view: AppCompatTextView) {

        fun show(show: Boolean): Right {
            if (!show) {
                view.visibility = View.GONE
            } else {
                view.visibility = View.VISIBLE
            }
            return this
        }

        fun drawable(@DrawableRes drawableId: Int, @ColorInt color: Int): Right {
            ContextCompat.getDrawable(view.context, drawableId)?.also {
                val wrap = DrawableCompat.wrap(it).mutate()
                DrawableCompat.setTint(wrap, color)
                view.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, wrap, null)
            }
            return this
        }

        fun view(): AppCompatTextView {
            return view
        }

        fun onClick(onClick: OnClickListener): Right {
            view.clickWithTrigger { onClick.onClick(it) }
            return this
        }
    }
}